import storage from '@/utils/storage.js';
import { allJournalApi, queryJournalApi } from '@/servers/journalServer.js';
import { idConvertPath } from '@/config/journalIcon';

const journalStore = {
    namespaced: true,
    state: {
        journalListOption:{} , // 请求日记列表参数
        journalList: [], // 日记列表
        pageOption:{
            user_id:storage.getCache("userInfo").id,
            limit:6,
            offset:0
        } , // 请求全部日记 需要用到的页码参数

    },
    mutations: {
        getjournalList(state, data) {
            state.journalList = data;
        },
        getjournalListOption(state,data){
            state.journalListOption = data
        },
        getpageOption(state,data){
            state.pageOption = data
        }
    },
    actions: {
        async getjournalListAction(context){
            const res = await queryJournalApi(context.state.journalListOption)
            let stateData = res.data
            stateData = idConvertPath(stateData)
            context.commit("getjournalList" , stateData)
        },
        getjournalListOptionAction(context,data){
            context.commit('getjournalListOption' , data)
        },
        getpageOptionAction(context , data){
            context.commit("getpageOption" , data)
        },
        async getAllJournalAction(context){
            const res = await allJournalApi(context.state.pageOption)
            let stateData = res.data
            stateData = idConvertPath(stateData)
            if(context.state.journalList.length === stateData.length){
                uni.$emit('noneJournal', {msg:"没有更多数据了~"})
            }
            context.commit("getjournalList" , stateData)
        }
    }
}
export default journalStore